package mju.cxf.config.scheduleds;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import mju.cxf.entity.Bill;
import mju.cxf.entity.Order;
import mju.cxf.entity.Sales;
import mju.cxf.entity.User;
import mju.cxf.service.BillService;
import mju.cxf.service.OrderService;
import mju.cxf.service.SalesService;
import mju.cxf.service.UserService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderScheduled {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private SalesService salesService;
    @Autowired
    private BillService billService;

    /**
     * 是否七天后自动完成
     * 开发仅用十分钟
     */
    @Scheduled(cron = "0 */1 * * * *") //每天0时30分0秒
    public void salesCancel() {
        LambdaQueryWrapper<Order> suw = new LambdaQueryWrapper<>();
        LocalDateTime now = LocalDateTime.now();
        // 十分钟前的时间
        LocalDateTime tenMinutesAgo = now.minus(5, ChronoUnit.MINUTES);
        suw.eq(Order::getStatus, 1) // 订单状态为已支付
                .lt(Order::getPaymentTime, tenMinutesAgo);
        orderService.list(suw).forEach(order -> {
            order.setStatus(2);
            order.setCompletionTime(LocalDateTime.now());
            LambdaQueryWrapper<Sales> lw = new LambdaQueryWrapper<>();
            lw.eq(Sales::getOrderNumber, order.getNumber()).eq(Sales::getState,4);
            salesService.list(lw).forEach(sales -> {
               double amount= sales.getAmount();
               int userId= sales.getUserId();
               LambdaUpdateWrapper<User> uw = new LambdaUpdateWrapper<>();
               User user = userService.getById(userId);
               uw.eq(User::getId, userId).setSql("available_balance = available_balance + " + amount).setSql("freeze_balances = freeze_balances - " + amount);
               userService.update(user, uw);
                //卖家出帐冻结金额
                Bill bill = Bill.builder()
                        .topic("订单冻结金额解除")
                        .type(2)
                        .balancesType(1)
                        .createTime(LocalDateTime.now())
                        .number(amount)
                        .userId(userId)
                        .build();
                billService.save(bill);
                //卖家入账金额
                Bill bill1 = Bill.builder()
                        .topic("订单完成")
                        .type(1)
                        .balancesType(2)
                        .createTime(LocalDateTime.now())
                        .number(amount)
                        .userId(userId)
                        .build();
                billService.save(bill1);
            });
            orderService.updateById(order);
        });


    }

}
